const app=require('express')()
const http=require('http').createServer(app)
const io=require('socket.io')(http)
const path=require('path')

const imgConvert=require('image-convert')
const { redirect } = require('express/lib/response')

function init(ebus){
    app.get('/gif2jpg/',(req,res)=>{
        console.log('gif2jpg')
        let url=req.query.url
        imgConvert.fromURL({
            url:url,
            quality: 100,//default 100
            output_format:"jpg",//default jpg
            size: 'original'//default original
        },(err,buffer,file)=>{
            if(!err)
            {
                res.send(buffer)
            }
        })
    })
    app.get('/',(req,res)=>{
        res.sendFile(path.join(__dirname,'index.html'))
    })
   
    io.on('connection',socket=>{
        console.log('webServer a user connected')
        socket.on('disconnect',()=>{
            console.log('user disconnected')
        })
    })
    http.listen(4000,()=>{
        console.log('webServer listening on 4000')
    })
    ebus.on('cmd',(data) => {
            
        io.emit('message',data.message)
        
    })
}

module.exports={
    init
}